package com.awspaas.user.apps.processforsap.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListener;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListenerInterface;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.processforsap.util.HttpClientGwUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TaskValidateForMM004 extends InterruptListener implements InterruptListenerInterface {
    @Override
    public boolean execute(ProcessExecutionContext ctx) throws Exception {
        /**
         * 电子
         */
        boolean isty = ctx.isChoiceActionMenu("提交");
        if (isty) {
            //取值
            BO bo = ctx.getBO("BO_EU_SAPPR_APPLY");
            String bsart = bo.getString("BSART");
            String ekorg = bo.getString("EKORG");
            List<BO> list = SDK.getBOAPI().query("BO_EU_SAPPR_PROLIST").bindId(bo.getBindId()).list();
            if (list == null || list.size() == 0) {
                throw new BPMNError("0331", "请填写物料信息！");
            }
            Set<String> groups = new HashSet<>();
            for (BO bo1 : list) {
                String matkl = bo1.getString("MATKL");
                String meins = bo1.getString("MEINS");
                if (matkl.startsWith("8-")) {
                    if (matkl.startsWith("8-BB")) {
                        if (!"SET".equals(meins)) {
                            throw new BPMNError("0331", "当物料组为8-BB时，单位只能为套（SET）！");
                        }
                    } else if (matkl.startsWith("8-JT")) {
                        if (!"UNT".equals(meins)) {
                            throw new BPMNError("0331", "当物料组为8-JT时，单位只能为台（UNT）！");
                        }
                    } else if (matkl.startsWith("8-YS")) {
                        if (!"VEH".equals(meins)) {
                            throw new BPMNError("0331", "当物料组为8-YS时，单位只能为辆（VEH）！");
                        }
                    } else {
                        if (!"ITM".equals(meins)) {
                            throw new BPMNError("0331", "当物料组为8-且不为8-BB/8-JT/8-YS，单位只能为项（ITM）！");
                        }
                    }


                }


//                String splistMatkl = matkl.substring(0, 1);
//                groups.add(splistMatkl);
//            }
//            if (groups.size() > 1) {
//                throw new BPMNError("0331", "物料组请保持同一类型");
//            }


                groups.add(matkl);
            }
            int pd = 0;
            boolean pd1 = groups.contains("9-10-00");
            if (pd1){
                pd=pd+1;
            }
            boolean pd2 = groups.contains("9-10-26");
            if (pd2){
                pd=pd+1;
            }
            boolean pd3 = groups.contains("9-20-01");
            if (pd3){
                pd=pd+1;
            }
            if (pd>1){
                throw new BPMNError("0331", "物料组9-10-00/9-10-26/9-20-01不能一起提交");
            }


            if (bsart.equals("ZPR1") || bsart.equals("ZPR3") || bsart.equals("ZPR5")) {
                if (list == null || list.size() == 0) {
                    throw new BPMNError("0331", "当请购类型为【普通请购单】、【NPI_研发工程料请购】、【公司间采购请购单】时，物料信息必填！");
                }
                for (BO bo1 : list) {
                    String matnr = bo1.getString("MATNR");
                    if (StringUtils.isEmpty(matnr)) {
                        throw new BPMNError("0331", "当请购类型为【普通请购单】、【NPI_研发工程料请购】、【公司间采购请购单】时，物料编码必填！");
                    }
                }
            }
            String werks = "";
            String result11 = bo.getString("BSART");
            if (result11.equals("ZPR7") || result11.equals("ZPR8") || result11.equals("ZPRC") || result11.equals("ZPRD")) {
                for (BO bo1 : list) {
                    String anln1 = bo1.getString("ANLN1");
                    if (StringUtils.isEmpty(anln1)) {
                        throw new BPMNError("0331", "当物料信息中，科目分配类别为【资产】时，资产编号必填！");
                    }
                    double num = bo1.get("MENGE", Double.class);
                    if (num != 1.000) {
                        throw new BPMNError("0331", "当物料信息中，科目分配类别为【资产】时，申请数量仅能为1！");

                    }
                }
            }
            if ("总务采购".equals(ekorg)) {
                for (BO bo1 : list) {
                    String a1 = bo1.getString("TEXT1");
                    String a2 = bo1.getString("MENGE");
                    String a3 = bo1.getString("MEINS");
                    String a4 = bo1.getString("PREIS");
                    String a5 = bo1.getString("WAERS");
                    String a6 = bo1.getString("MATKL");
                    String a7 = bo1.getString("WERKS");
                    if (StringUtils.isEmpty(a1) || StringUtils.isEmpty(a2) || StringUtils.isEmpty(a3) || StringUtils.isEmpty(a4) || StringUtils.isEmpty(a5) || StringUtils.isEmpty(a6) || StringUtils.isEmpty(a7)) {
                        throw new BPMNError("0331", "受理部门为：总务采购时，短文本、申请数量、计量单位、评估价格、币种、物料组、工厂必填！");
                    }
                }
            }
        }
        return true;
    }
}
